package com.lamber.framework.response;

import com.lamber.framework.util.ExceptionHelper;

import java.io.Serializable;

public class ResponseMessage<T> implements Serializable {
    private static final long serialVersionUID = 8992987576262574064L;

    private Long timestamp;

    private Integer code;

    private String message;

    private T data;

    private Object pager;

    private ResponseMessage() {
    }

    public static ResponseMessage ok() {
        return ok((Object) null, (Object) null);
    }

    public static ResponseMessage ok(Object data) {
        return ok((Object) null, data);
    }

    public static ResponseMessage ok(Object pager, Object data) {
        ResponseMessage msg = new ResponseMessage();
        msg.setTimeStamp().setCode(200).setPager(pager).setData(data);
        return msg;
    }

    public static ResponseMessage error(Exception ex) {
        return error(500, ExceptionHelper.getBootMessage(ex));
    }

    public static ResponseMessage error(String message) {
        return error(500, message);
    }

    public static ResponseMessage error(int code, String message) {
        ResponseMessage msg = new ResponseMessage();
        msg.setCode(code).setMessage(message).setTimeStamp();
        return msg;
    }

    private ResponseMessage setTimeStamp() {
        this.setTimeStamp(System.currentTimeMillis());
        return this;
    }

    private ResponseMessage setTimeStamp(Long timeStamp) {
        this.timestamp = timeStamp;
        return this;
    }

    public ResponseMessage setCode(int code) {
        this.code = code;
        return this;
    }

    public ResponseMessage setMessage(String message) {
        this.message = message;
        return this;
    }

    public ResponseMessage setPager(Object pager) {
        this.pager = pager;
        return this;
    }

    public ResponseMessage<T> setData(T data) {
        this.data = data;
        return this;
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return data;
    }

    public Long getTimestamp() {
        return this.timestamp;
    }

    public Object getPager() {
        return this.pager;
    }

    public boolean isSuccess() {
        return this.code == 200;
    }
}
